<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>首页</title>
  <style>
    *,
    *::after,
    *::before {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    :root {
      min-height: 100vh;
    }

    :root:focus-within {
      scroll-behavior: smooth;
    }

    body {
      min-height: inherit;
    }

    .noticeBox {
      width: 100%;
      display: flex;
      flex-direction: row;
      flex-wrap: nowrap;
      overflow: hidden;
    }

    .noticeBox>.noticeTxt {
      word-break: keep-all;
      white-space: nowrap;
      animation: loopAnimation 10s linear infinite both;
      text-align: right;
      /* 这一句很重要，为了能让滚动左右连接起来 */
      padding-left: 100%;
    }

    @keyframes loopAnimation {
      0% {
        transform: translate3d(0, 0, 0);
      }

      100% {
        transform: translate3d(-100%, 0, 0);
      }
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  "use strict"; {
    let list = [
      "锦瑟无端五十弦，一弦一柱思华年",
      "庄生晓梦迷蝴蝶，望帝春心托杜鹃",
      "沧海月明珠有泪，蓝田日暖玉生烟"
    ];
    // 文本移动速度
    let speed = 100

    // 动画的开始和结束执行
    let animationPlayState = ref("paused")
    let animationDuration = ref("10s")

    let noticeTxt = computed(() => {
      return list.join('，')
    })

    let getSize = () => {
      let query = uni.createSelectorQuery().select(".noticeTxt")
      query.boundingClientRect((data) => {
        let textWidth = data?.width
        if (textWidth == undefined) return
        // 水平滚动时的滚动速度，即每秒滚动多少rpx，这有利于控制文字无论多少时，都能有一个恒定的速度
        // 根据t=s/v(时间=路程/速度)，这里为何不需要加上 noticeTxt 的宽度，因为中设置了 样式中设置了padding-left: 100%
        animationPlayState.value = "running"
        animationDuration.value = Math.floor((textWidth / speed)) + "s"
      }).exec()
    }

    onMounted(() => {
      getSize()
    })

  };
  /*
  <view class="noticeBox">
    <view class="noticeTxt" :style='{animationDuration:animationDuration,animationPlayState:animationPlayState,}'>
      {{noticeTxt}}
    </view>
  </view>
  */



  <script setup lang="ts">
    import {computed, onMounted, reactive, ref} from "vue"

    let list = [
    "锦瑟无端五十弦，一弦一柱思华年",
    "庄生晓梦迷蝴蝶，望帝春心托杜鹃",
    "沧海月明珠有泪，蓝田日暖玉生烟"
    ];
    // 文本移动速度
    let speed = 100

    // 动画的开始和结束执行
    let animationPlayState = ref("paused")
    let animationDuration = ref("10s")

	let noticeTxt = computed(() => {
		return list.join('，')
	})

	let getSize = () => {
      let query = uni.createSelectorQuery().select(".noticeTxt")
		query.boundingClientRect((data : any) => {
      let textWidth = data?.width
    if (textWidth == undefined) return
    // 水平滚动时的滚动速度，即每秒滚动多少rpx，这有利于控制文字无论多少时，都能有一个恒定的速度
    // 根据t=s/v(时间=路程/速度)，这里为何不需要加上 noticeTxt 的宽度，因为中设置了 样式中设置了padding-left: 100%
    animationPlayState.value = "running"
    animationDuration.value = Math.floor((textWidth / speed)) + "s"
		}).exec()
	}

	onMounted(() => {
      getSize()
    })

    function moveFun() {
      animationPlayState.value = (animationPlayState.value == "paused" ? "running" : "paused")
    }
</script>

<template>
  <view class="noticeBox">
    <view class="noticeTxt" :style='{animationDuration:animationDuration,animationPlayState:animationPlayState,}'>
      {{noticeTxt}}
    </view>
  </view>

  <view class="btn" @tap="moveFun"></view>
</template>

<style lang="scss" scoped>
  .btn {
    background-color: yellow;
    width: 100rpx;
    height: 100rpx;
  }

  .noticeBox {
    width: 100%;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    overflow: hidden;
  }

  .noticeBox>.noticeTxt {
    word-break: keep-all;
    white-space: nowrap;
    animation: loopAnimation 10s linear infinite both;
    text-align: right;
    // 这一句很重要，为了能让滚动左右连接起来
    padding-left: 100%;
  }

  @keyframes loopAnimation {
    0% {
      transform: translate3d(0, 0, 0);
    }

    100% {
      transform: translate3d(-100%, 0, 0);
    }
  }
</style>
</script>

</html>